package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.schoolCommunity.entity.ConfessionWall;
import com.schoolCommunity.entity.ConfessionWallMessage;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.mapper.ConfessionWallMapper;
import com.schoolCommunity.mapper.ConfessionWallMessageMapper;
import com.schoolCommunity.service.ConfessionWallMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-09-09
 */
@Service
public class ConfessionWallMessageServiceImpl extends ServiceImpl<ConfessionWallMessageMapper, ConfessionWallMessage> implements ConfessionWallMessageService {

    @Resource
    private ConfessionWallMessageMapper ConfessionWallMessageMapper;

    @Autowired
    @Qualifier("MyTemplate")
    private RedisTemplate redisTemplate;

    Logger log = Logger.getLogger(ConfessionWallMessageServiceImpl.class);

    //写入某一条留言
    @Override
    public Information InsertConfessionWallMessage(ConfessionWallMessage ConfessionWallMessage) {

        Information Information = new Information();

        int insert = ConfessionWallMessageMapper.insert(ConfessionWallMessage);

        if (insert != 0) {
            Information.setCode(200).setMessage("留言成功");
        } else {
            Information.setMessage("留言失败").setCode(404);
        }
        return Information;

    }

    //删除某一条留言
    @Override
    public Information DeleteConfessionWallMessage(long id) {
        Information Information = new Information();

        int i = ConfessionWallMessageMapper.deleteById(id);

        if (i != 0) {
            Information.setCode(200).setMessage("该留言已经删除");
        } else {
            Information.setMessage("留言删除失败").setCode(404);
        }
        return Information;
    }

    //根据表白墙id获取留言
    @Override
    public Information GetConfessionWallMessage(long ConfessionWallId) {

        Information Information = new Information();

//        Object ConfessionWallMessage = redisTemplate.opsForValue().get("GetConfessionWallMessage:" + ConfessionWallId);
//
//        if(ConfessionWallMessage==null){

//            log.info("redis没有数据，从数据库中获取");

        QueryWrapper QueryWrapper = new QueryWrapper();
        QueryWrapper.eq("confession_wall_id", ConfessionWallId);
        QueryWrapper.eq("Suggest", 1);
        QueryWrapper.select("id", "text", "member_id", "member_nickname", "show_nicknane", "isSecret", "confession_wall_id", "create_time");
        List list = ConfessionWallMessageMapper.selectList(QueryWrapper);

        if (list.isEmpty()) {
            log.info("数据库中没有关于此表白墙的留言");
            Information.setMessage("此表白墙没有留言").setCode(404);
        } else {

            log.info("获取到了数据，下面输出");
            log.info("获取的数据写入redis");
//                redisTemplate.opsForValue().set("GetConfessionWallMessage:"+ConfessionWallId,list);
            Information.setMessage("获取指定表白墙下面的留言成功").setCode(200).setData(list);
        }

//        }else{
//            System.out.println("redis中找到了数据");
//            List<ConfessionWallMessage> list = (List<com.schoolCommunity.entity.ConfessionWallMessage>) ConfessionWallMessage;
//            Information.setData(list).setCode(200).setMessage("获取指定表白墙下面的留言成功");

//        }
        return Information;
    }

    //获取指定用户的留言
    @Override
    public Information GetConfessionWallMessageByMemberId(long MemberId) {

        Information information = new Information();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id", MemberId);
        queryWrapper.select("id", "text", "member_id", "member_nickname", "show_nicknane", "isSecret", "confession_wall_id", "create_time", "Suggest");

        List list = ConfessionWallMessageMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            information.setCode(200).setMessage("指定用户留言查询成功").setData(list);
        } else {
            information.setMessage("此用户没有留言数据或留言被删除").setCode(404);
        }

        return information;
    }

    //管理员获取尚未审核的留言
    @Override
    public Information GetNotSuggestConfessionWallMessage() {

        Information information = new Information();

        QueryWrapper QueryWrapper = new QueryWrapper();
        QueryWrapper.eq("Suggest", 0);

        List list = ConfessionWallMessageMapper.selectList(QueryWrapper);
        if (!list.isEmpty()) {
            log.info("数据库中还有" + list.size() + "条留言尚未审核");

            information.setMessage("数据库中还有" + list.size() + "条留言尚未审核").setCode(200).setData(list);

        } else {
            log.info("数据库没有需要审核的留言");
            information.setCode(200).setMessage("数据库没有需要审核的留言");
        }


        return information;
    }

    //管理员审核留言
    @Override
    public Information AdminSuggestConfessionWallMessage(long confessionWallMessageId, int suggestResult) {

        Information information = new Information();

        QueryWrapper QueryWrapper = new QueryWrapper();
        QueryWrapper.eq("id", confessionWallMessageId);

        int update = ConfessionWallMessageMapper.update(new ConfessionWallMessage().setSuggest(suggestResult), QueryWrapper);

        if (update != 0) {
            information.setMessage("用户留言审批成功").setCode(200);
        } else {
            information.setMessage("用户留言审批不通过").setCode(404);
        }
        return information;


    }
}
